import numpy as np
import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('smart car.jpeg',0)
dft = cv2.dft(np.float32(img1),flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换（零频率分量不在中心位置）
dftShift = np.fft.fftshift(dft) # 傅里叶变换（零频率分量在中心位置）

rows, cols = img1.shape
row_center, col_center = int(rows/2) , int(cols/2) # 图像中心

# 理想低通滤波器
mask = np.zeros((rows, cols, 2), np.float32)
mask[row_center-60:row_center+60, col_center-60:col_center+60] = 1
img3 = cv2.magnitude(mask[:,:,0], mask[:,:,1])

convolution = dftShift * mask # 卷积

idftshift = np.fft.ifftshift(convolution)
idft = cv2.idft(idftshift)
img2 = cv2.magnitude(idft[:,:,0], idft[:,:,1])

plt.figure(figsize=(10,4),dpi=120)
plt.subplot(131),plt.imshow(img1, cmap = 'gray')
plt.title('original'),plt.axis('off')
plt.subplot(132),plt.imshow(img3, cmap = 'gray')
plt.title('mask'), plt.axis('off')
plt.subplot(133),plt.imshow(img2, cmap = 'gray')
plt.title('ILPF'), plt.axis('off')
plt.show()